package com.lai.qq.admin.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lai.qq.admin.modules.system.mapper.MenuMapper;
import com.lai.qq.admin.modules.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.qq.admin.modules.system.dto.input.MenuQueryPara;
import com.lai.qq.admin.modules.system.entity.Menu;

/**
 * <p>
 * 系统管理-菜单表 服务实现类
 * </p>
 *
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> listTreeMenu() {
        return menuMapper.selectList(null);
    }

    @Override
    public void listPage(IPage<Menu> page, MenuQueryPara filter) {
        page.setRecords(menuMapper.selectMenus(page, filter));
    }

    @Override
    public List<Menu> list(MenuQueryPara filter) {
        return menuMapper.selectMenus(filter);
    }

    @Override
    public Integer saveOrUpdateData(Menu para) {
        if (para.getId() != null) {
            menuMapper.updateById(para);
        } else {
            menuMapper.insert(para);
        }
        return para.getId();
    }

    @Override
    public List<Menu> getPermissionByUserId(String userId) {
        return menuMapper.getPermissionByUserId(userId);
    }


    @Override
    public List<String> getPerms(String userId) {
        List<String> perms = new ArrayList<>();
        List<Menu> sysPermissions = getPermissionByUserId(userId);
        sysPermissions.stream().forEach(sysPermission -> {
            perms.add(sysPermission.getPath());
        });
        return perms;
    }
}
